package com.android.library.adapter;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;

/**
 * @author xiaoxing
 * Description RecyclerView触摸事件处理，这里做的是移动处理
 * CreateData: 2020/11/10
 */
public class CustomItemTouchCallback extends ItemTouchHelper.Callback {
    private  ItemTouchHelperAdapter mAdapter;

    public CustomItemTouchCallback(ItemTouchHelperAdapter mAdapter) {
        this.mAdapter = mAdapter;
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        int dragFlags;
        int swipeFlags = 0;
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        } else {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        }
        return makeMovementFlags(dragFlags,swipeFlags);
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        //通知adapter更新数据和视图
        mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
        //若返回false则表示不支持上下拖拽
        return true;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        //通知adapter更新数据和视图
        mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        //是否可以左右侧滑，默认返回true
        return false;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        //是否可以长按上下拖拽，默认返回false
        return false;
    }
}
